Public Class DataGridViewLookUpComboBoxCell
    Inherits DataGridViewTextBoxCell


    'Private dgvOwningColumn As DataGridViewLookUpComboBoxColumn
    Private _LookUpList As LookUpList
    Private _DropDownStyle As System.Windows.Forms.ComboBoxStyle = ComboBoxStyle.DropDown

    Private Function GetLookupList() As LookUpList
        
        If _LookUpList Is Nothing Then
            If Not Me.OwningColumn Is Nothing AndAlso Not DirectCast(Me.OwningColumn, DataGridViewLookUpComboBoxColumn).LookupList Is Nothing Then
                Return DirectCast(Me.OwningColumn, DataGridViewLookUpComboBoxColumn).LookupList
            Else
                Return Nothing
            End If
        Else
            Return _LookUpList
        End If
    End Function



    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

        'dgvOwningColumn = DirectCast(Me.OwningColumn, DataGridViewLookUpComboBoxColumn)
        Dim cmbEditCtrl As DataGridViewLookUpComboBoxEditControl


        cmbEditCtrl = DirectCast(DataGridView.EditingControl, DataGridViewLookUpComboBoxEditControl)
        cmbEditCtrl.DropDownStyle = _DropDownStyle

        cmbEditCtrl.DataSource = GetLookupList()
        If Not initialFormattedValue Is Nothing Then
            cmbEditCtrl.Text = DirectCast(initialFormattedValue, String)
        Else
            cmbEditCtrl.Text = ""
        End If



    End Sub

    Public Overrides ReadOnly Property FormattedValueType() As System.Type
        Get
            Return GetType(String)
        End Get
    End Property

    Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
        If String.IsNullOrEmpty(CStr(value)) Then Return Nothing

        Dim tmpLUlist As LookUpList = GetLookupList()

        If Not tmpLUlist Is Nothing Then
            Dim tmpIndex As Integer = tmpLUlist.IndexOfID(CInt(value))
            If tmpIndex >= 0 Then
                Return tmpLUlist(tmpLUlist.IndexOfID(CInt(value))).Name
            Else
                Return Nothing
            End If
        Else
            Return Nothing
        End If
    End Function

    Public Overrides ReadOnly Property ValueType() As System.Type
        Get
            Return GetType(Integer)
        End Get
    End Property

    Public Overrides Function ParseFormattedValue(ByVal formattedValue As Object, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal valueTypeConverter As System.ComponentModel.TypeConverter) As Object
        If Not formattedValue Is Nothing Then
            Return GetLookupList.IDbyValue(DirectCast(formattedValue, String))
        End If
        Return Nothing
    End Function

    Public Overrides ReadOnly Property EditType() As Type
        Get
            Return GetType(DataGridViewLookUpComboBoxEditControl)
        End Get
    End Property

    Public Property LookupList() As LookUpList
        Get
            Return GetLookupList()
        End Get
        Set(ByVal value As LookUpList)
            _LookUpList = value
        End Set
    End Property

    Public Property DropDownStyle() As System.Windows.Forms.ComboBoxStyle
        Get
            Return _DropDownStyle
        End Get
        Set(ByVal value As System.Windows.Forms.ComboBoxStyle)
            _DropDownStyle = value
        End Set
    End Property
End Class
